/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.web.controller;

import com.opgea.qms.constraints.SessionConstraints;
import com.opgea.qms.domain.model.JsonModelMap;
import com.opgea.qms.service.TokenDetailsService;
import com.opgea.qms.service.TokenService;
import com.opgea.qms.web.form.TokenDetailsDTO;
import com.opgea.qms.web.model.ComboModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Ramesh
 */
@Controller
@RequestMapping(value="/reception")
public class ReceptionController {
    
    @Autowired
    private TokenDetailsService tokenDetailsService;
    
    @Autowired
    private TokenService tokenService;
    
    @RequestMapping(method= RequestMethod.GET)
    public String show(){
        return "receptionViewport";
    }
    
    @RequestMapping(value="prefixList", method=RequestMethod.GET)
    public @ResponseBody Map<String, Object> prefixList(HttpServletResponse res){
        //res.setContentType("application/json");
        
        List<ComboModel> prefixList = new ArrayList<ComboModel>();
        prefixList.add(new ComboModel("1","Mr"));
        prefixList.add(new ComboModel("2","Miss"));
        prefixList.add(new ComboModel("3","Mrs"));
        
        Map<String, Object> prefixMap = new HashMap<String, Object>();
        prefixMap.put("success", true);
        prefixMap.put("data", prefixList);
       
        return prefixMap;
    }
    
    @RequestMapping(value="tokenList", method=RequestMethod.GET)
    public @ResponseBody Map<String, Object> getTokenList(
                                                @RequestParam(value="companyId", required=false)String compId, 
                                                @RequestParam(value="tokenDate", required=false)String tokenDate, 
                                                @RequestParam(value="searchKey", required=false)String searchKey, 
                                                HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        //List<TokenDetailsDTO> tokenList = tokenService.findAllByCompanyId(companyId, Calendar.getInstance().getTime());
        
        List<TokenDetailsDTO> tokenList = tokenDetailsService.searchTokens(companyId, searchKey, tokenDate);
        System.out.println("Token List: "+tokenList.size());
        return JsonModelMap.success().data(tokenList);
    }
    
    @RequestMapping(value="createToken", method=RequestMethod.POST)
    public @ResponseBody 
            Map<String, Object> createToken(TokenDetailsDTO tokenDetailsDTO, HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        
        tokenDetailsDTO.setCompanyId(companyId);
        tokenDetailsService.update(tokenDetailsDTO);
        
        //return JsonModelMap.success().data("Token saved successfully!<br><b>"+tokenDetailsDTO.getTokenNo()+"</b>");
        return JsonModelMap.success().data(tokenDetailsDTO);
    }
    
    
    
}
